﻿/******************************************************   
创建者: ljj
创建时间: 2018-07-06
修改:
修改时间: 
功能: 导入导出店铺规划SKC
 ******************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm;
using com.nm.library.Excels;
using george.Helper;

namespace VerticaAPI.Export
{
    [Export(typeof(ExcelImport))]
    public class DWShopStandardSKC : ExcelImport
    {
        private static int num = 0;

        public override ExcelImportType Type => ExcelImportType.DWSHOPSTANDARSKC;
        public override Dictionary<string, ImportVerify> DictFields => _dictFields;

        private Dictionary<string, ImportVerify> _dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="店铺代码",FieldName="ShopCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="店铺名称",FieldName="ShopName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="大类",FieldName="KindName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="中类",FieldName="ModelName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="小类",FieldName="ItemName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="细类",FieldName="SubItem",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="现有SKC",FieldName="HaveSKC",DataType=typeof(long),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="标准陈列SKC",FieldName="StandardSKC",DataType=typeof(long),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
        }.ToDictionary(e => e.ColumnName, e => e);

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var handler = new DWShopStandardSKCHandler();
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                var errorDT = handler.ImportShopStandardSKC(dt);
                result.Message =
                    $"成功导入{(dt.Rows.Count - errorDT.Rows.Count)}条数据{(errorDT.Rows.Count == 0 ? "" : $@",有{errorDT.Rows.Count}条错误数据")}";
                if (errorDT.Rows.Count != 0)
                {
                    var path = ExcelImporMapper.GetTemplatePath(Type);
                    result.Url = ExcelUtil.ExportErrorExeclToDB(errorDT, path, _dictFields, "店铺规划SKC错误数据", num);
                    num++;
                }
            }
            catch (Exception ex)
            {
                Log.Error("导入店铺规划SKC：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            return result;
        }

        public override DataTable GetExportData(string Keys)
        {
            var keyStr = "";
            if (!string.IsNullOrEmpty(Keys))
            {
                const string startStr = "and KeyStart = '";
                const string endStr = "KeyEnd'";
                Keys = Keys.GetSubString(startStr, endStr, out keyStr);
            }
            var handler = new DWShopStandardSKCHandler();
            return handler.ExportShopStandardSKC(Keys, keyStr);
        }
    }
}
